package com.person.ntv.shiro.util;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.person.ntv.entity.sys.SysUser;

@Service
public class PasswordHelper {
	
	  private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
	    @Value("${password.algorithmName}")
	    private String algorithmName = "md5";
	    @Value("${password.hashIterations}")
	    private int hashIterations = 2;

	    public void setRandomNumberGenerator(RandomNumberGenerator randomNumberGenerator) {
	        this.randomNumberGenerator = randomNumberGenerator;
	    }

	    public void setAlgorithmName(String algorithmName) {
	        this.algorithmName = algorithmName;
	    }

	    public void setHashIterations(int hashIterations) {
	        this.hashIterations = hashIterations;
	    }
	    public  void encryptPassword(SysUser user) {  
	        user.setPwdSalt(randomNumberGenerator.nextBytes().toHex());  
	        String newPassword = new SimpleHash(
	                algorithmName,  
	                user.getUserPassword(),  
	                ByteSource.Util.bytes(user.getCridentialSalt()),hashIterations).toHex();  
	        user.setUserPassword(newPassword);  
	}
	    public  String getEncryptPassword(SysUser user) {  
	        String newPassword = new SimpleHash(
	                algorithmName,  
	                user.getUserPassword(),  
	                ByteSource.Util.bytes(user.getCridentialSalt()),hashIterations).toHex();  
	      return newPassword;  
	}  
}
